<?php

/**
 * This is the model class for table "user".
 *
 * The followings are the available columns in table 'user':
 * @property integer $id
 * @property string $username
 * @property string $password
 * @property string $email
 * @property integer $active
 *
 * The followings are the available model relations:
 * @property Block[] $blocks
 */
class User extends ActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return User the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'user';
	}

	public function scopes()
	{
	    return array(
	            'active'=>array(
	                    'condition'=>'active=1',
	            ),

	    );
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		return array_merge(parent::rules(), array(
			array('username, password, email, active', 'required',  'message'=>'Kötelező kitölteni'),
			array('active', 'numerical', 'integerOnly'=>true),
			array('username, password, email', 'length', 'max'=>255),
		    array('email', 'email', 'message'=>'Nem valódi e-mail cím.'),

			array('username, email, active', 'safe', 'on'=>'search'),
		));
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		return array(
			'blocks' => array(self::HAS_MANY, 'Block', 'user_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'username' => 'Felhasználónév',
			'password' => 'Jelszó',
			'email' => 'E-mail',
			'active' => 'Aktív',
		);
	}

	public function hashPassword($pass){
	    return $this->password = $this->makePassword($pass);
	}

	/**
	 *
	 * @param string $pass password
	 * @return string hashed password
	 */
	public function makePassword($pass){
	    Yii::log(params('salt'), 'debug');
	    return md5(params('salt') . $pass);
	}

	public function searchFields()
	{
        return array('username', 'email');
	}
}